package com.example.administrator.liangcang.message.activity;

import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.example.administrator.liangcang.R;
import com.example.administrator.liangcang.databinding.ActivityMessageTopBinding;
import com.example.administrator.liangcang.message.AssortmentFragment;
import com.example.administrator.liangcang.message.AuthorFragment;
import com.example.administrator.liangcang.until.CommentPagerFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

public class MessageTopActivity extends AppCompatActivity {
    private ActivityMessageTopBinding binding;
    private List<Fragment> fragments ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_message_top);
        aboutTabLayout();
        aboutFinishActivity();
    }

    private void aboutTabLayout() {
        String[] titles = new String[]{"分类","作者"};
        fragments = new ArrayList<>();
        AssortmentFragment assortmentFragment = new AssortmentFragment();
        fragments.add(assortmentFragment);
        AuthorFragment authorFragment = new AuthorFragment();
        fragments.add(authorFragment);
        CommentPagerFragmentAdapter adapter = new CommentPagerFragmentAdapter(getSupportFragmentManager(), fragments, titles);
        binding.pager.setAdapter(adapter);
        binding.tabLayout.setupWithViewPager(binding.pager);
    }

    private void aboutFinishActivity() {
        binding.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(0,R.anim.activity_exit);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            finish();
            overridePendingTransition(0,R.anim.activity_exit);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
